﻿using System;
using System.Data;
using System.Web;
using System.Text;
using System.IO;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using System.Reflection;
using System.Data.SqlClient;
using log4net;
using Webdiver;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.system.res_manage
{
    public partial class callback : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
   
        protected void btnGarbage_Click(object sender, EventArgs e)
        {
            Server.ScriptTimeout = 600;
            string commandText = "SELECT [AttachmentId],[Name] FROM [dbo].[Attachment] WHERE [DeleteFlag] = 1";
            DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    try
                    {
                        AttachmentBiz.DeleteSingleAttach(base.Conn, table.Rows[i]["AttachmentId"].ToString(), table.Rows[i]["Name"].ToString());
                        DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(AttachmentBiz.AttachmentPath + "/" + table.Rows[i]["AttachmentId"].ToString()));
                        if (AttachmentBiz.IsDirectoryEmpty(dirInfo))
                        {
                            AttachmentBiz.DeleteDirectory(dirInfo);
                        }
                    }
                    catch (Exception exception)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("", exception);
                        }
                    }
                }
            }
            ConfigManager manager = new ConfigManager();
            string sqlForResCallback = manager.SqlForResCallback;
            try
            {
                if (sqlForResCallback != string.Empty)
                {
                    SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, sqlForResCallback);
                }
            }
            catch (Exception exception2)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception2);
                }
            }
            base.CloseConn();
            Response.Redirect("callback.aspx");
        }
        protected void btnReadSms_Click(object sender, EventArgs e)
        {
            string commandText = "DELETE FROM [dbo].[Sms] WHERE [RemindFlag] = 0";
            try
            {
                SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, commandText);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            base.CloseConn();
            Response.Redirect("callback.aspx");
        }
        protected void btnSpecifyTime_Click(object sender, EventArgs e)
        {
            string commandText = string.Empty;
            if (this.ckbTimeSms.Checked)
            {
                commandText = "DELETE FROM [dbo].[Sms] WHERE 1=1 ";
                if (this.txtBeginTime.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendTime] >= @BeginTime ";
                }
                if (this.txtEndTime.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendTime] <= @EndTime ";
                }
            }
            if (this.ckbTimeEmail.Checked)
            {
                commandText = commandText + "UPDATE [dbo].[Attachment] SET [DeleteFlag]=1 WHERE AttachmentId IN(" + "SELECT [AttachmentId] FROM [dbo].[Email] WHERE ISNULL([AttachmentId],'') <> '' ";
                if (this.txtBeginTime.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendTime] >= @BeginTime ";
                }
                if (this.txtEndTime.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendTime] <= @EndTime ";
                }
                commandText = commandText + ") " + "DELETE FROM [dbo].[Email] WHERE 1=1 ";
                if (this.txtBeginTime.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendTime] >= @BeginTime ";
                }
                if (this.txtEndTime.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendTime] <= @EndTime ";
                }
            }
            ArrayList list = new ArrayList();
            if (this.txtBeginTime.Value != string.Empty)
            {
                string str2 = (this.txtBeginTime.Value.Length > 10) ? this.txtBeginTime.Value : (this.txtBeginTime.Value + " 00:00:00");
                list.Add(new object[] { "BeginTime", Convert.ToDateTime(str2) });
            }
            if (this.txtEndTime.Value != string.Empty)
            {
                string str3 = (this.txtEndTime.Value.Length > 10) ? this.txtEndTime.Value : (this.txtEndTime.Value + " 23:59:59");
                list.Add(new object[] { "EndTime", Convert.ToDateTime(str3) });
            }
            SqlParameter[] commandParameters = new SqlParameter[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                object[] objArray3 = (object[])list[i];
                commandParameters[i] = new SqlParameter("@" + objArray3[0].ToString(), objArray3[1]);
            }
            try
            {
                SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, commandText, commandParameters);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            base.CloseConn();
            Response.Redirect("callback.aspx");
        }
        protected void btnSpecifyUser_Click(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            string str = this.ToId.Value;
            if (str.EndsWith(","))
            {
                str = str.Substring(0, str.Length - 1);
            }
            string[] strArray = str.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                builder = new StringBuilder();
                if (this.ckbEmail.Checked)
                {
                    builder.Append("UPDATE [dbo].[Attachment] SET [DeleteFlag]=1 WHERE [AttachmentId] IN(");
                    builder.Append("SELECT [AttachmentId] FROM [dbo].[Email] WHERE ISNULL([AttachmentId],'') <> '' AND [FromId] = @UserId OR [ToId] = @UserId) ");
                    builder.Append("DELETE FROM [dbo].[Email] WHERE [FromId] = @UserId OR [ToId] = @UserId ");
                }
                if (this.ckbFileFolder.Checked)
                {
                    AttachmentBiz.DeleteDirectory(new DirectoryInfo(Server.MapPath("/attachment/file_folder/" + strArray[i])));
                }
                if (this.ckbAddress.Checked)
                {
                    builder.Append("DELETE FROM [dbo].[Address] WHERE [UserId] = @UserId ");
                    builder.Append("DELETE FROM [dbo].[Address_Group] WHERE [UserId] = @UserId ");
                }
                if (this.ckbCalendar.Checked)
                {
                    builder.Append("DELETE FROM [dbo].[Calendar] WHERE [UserId] = @UserId ");
                }
                if (this.ckbCalendar.Checked)
                {
                    builder.Append("DELETE FROM [dbo].[Diary_Comment] WHERE [DiaryId] IN( ");
                    builder.Append("SELECT [DiaryId] FROM [dbo].[Diary] WHERE [UserId] = @UserId) ");
                    builder.Append("DELETE FROM [dbo].[Diary] WHERE [UserId] = @UserId ");
                }
                if (this.ckbUrl.Checked)
                {
                    builder.Append("DELETE FROM [dbo].[Url] WHERE [UserId] = @UserId ");
                }
                commandParameters[0].Value = strArray[i];
                try
                {
                    if (builder.ToString() != string.Empty)
                    {
                        SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, builder.ToString(), commandParameters);
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
            base.CloseConn();
            Response.Redirect("callback.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.btnReadSms.Attributes.Add("onclick", "return delete_sms()");
                this.btnSpecifyTime.Attributes.Add("onclick", "return delete_specifytime()");
                this.btnSpecifyUser.Attributes.Add("onclick", "return delete_specifyuser()");
                this.btnGarbage.Attributes.Add("onclick", "return delete_garbage()");
                this.txtEndTime.Value = DateTime.Now.ToString();
            }
        }

    }
}
